#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2020-08-29 15:15:42
# @Author  : Zuo Zhongfei (zuozhongfei@126.com)
# @Link    :
# @Version : $Id$

import os
import pygame
from pygame.sprite import Sprite

class Ship(Sprite):
    """飞船类"""

    def __init__(self, ai_settings, screen):
        """初始化飞船并设置其初始位置"""
        super(Ship,self).__init__()
        self.screen = screen
        # 加载飞船图像并设置器外接矩形
        self.image = pygame.image.load("/Users/zuozhongfei/Desktop/学习/Python/基础/python-learning/2:项目/alien_invasion/images/ship.bmp")
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        self.ai_settings = ai_settings

        # 右移标志属性
        self.moving_right = False

        # 左移标志属性
        self.moving_left = False

        self.moving_up = False

        self.moving_down = False

        # 将每艘飞船放置在屏幕底部
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        self.center = float(self.rect.centerx)

    def update(self):
        # 根据移动标志调整飞船的位置
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.ai_settings.ship_speed_factor

        if self.moving_left and self.rect.left > 0:
            self.center -= self.ai_settings.ship_speed_factor

        if self.moving_up and self.rect.top > self.screen_rect.top:
            self.rect.centery -= self.ai_settings.ship_speed_factor

        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.rect.centery += self.ai_settings.ship_speed_factor
        # 根据self.center属性更新rect对象
        self.rect.centerx = self.center

    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def center_ship(self,screen):
        """让飞船在屏幕上居中"""
        self.center = self.screen_rect.centerx
        self.rect.bottom = screen.get_rect().bottom
